package com.lmb.service.impl;

import com.lmb.annotation.LogMethod;
import com.lmb.bean.AdministratorLogin;
import com.lmb.bean.UserLogin;
import com.lmb.mapper.AdministratorLoginDao;
import com.lmb.mapper.UserLoginDao;
import com.lmb.service.Login2Service;
import com.lmb.util.PasswordUtil;
import com.lmb.util.SyS;
import com.lmb.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Login2ServiceImpl implements Login2Service {

    @Autowired
    AdministratorLoginDao administratorLoginDao;

    public AdministratorLogin getlogin(String token){
        Object id= TokenUtil.getAdname(token, SyS.TOKEN_AD_ID);
        if (id==null){
            TokenUtil.removeToken(token);
            throw new RuntimeException("登录异常");

        }
        AdministratorLogin administratorLogin = administratorLoginDao.one(Integer.parseInt(id.toString()));
        return administratorLogin;
    }
    @Override
    @LogMethod(serviceName = "管理员登录")
    public String login(AdministratorLogin administratorLogin) {
        //登录逻辑
        //1.通过用户名查询数据库
        AdministratorLogin result=administratorLoginDao.getByadname(administratorLogin.getAd_name());
        if(result==null){
            return "用户不存在";
        }
        //2.判断密码
        String password=administratorLogin.getAd_password();
        String encryptionPassword= result.getAd_password();
        if(!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("密码错误");
        }
        String token= TokenUtil.getAdminToken(result.getAd_id(),result.getAd_name());
        //3.返回结果
        return token;
    }
}
